#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow ( QWidget* parent ) :
    QMainWindow ( parent ),
    ui ( new Ui::MainWindow )
{
    ui->setupUi ( this );

    for ( int i = 0; i < 10; i++ )
    {
        dm0[i] = new QQtDataCacheManager ( this );
        dm0[i]->setListeningPort ( 6090 + i );
    }

    m0.setServerPort ( 6500 );
    m0.sendFindServer();
    connect ( &m0, SIGNAL ( signalFindServer ( QString, int ) ),
              this, SLOT ( slotFindServer ( QString, int ) ) );

    m_timer = new QTimer ( this );
    m_timer->setSingleShot ( false );
    m_timer->setInterval ( 100 );
    connect ( m_timer, SIGNAL ( timeout() ),
              this, SLOT ( slotTimeout() ) );

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked ( bool checked )
{
    //start
    m_timer->start();
}

void MainWindow::on_pushButton_2_clicked ( bool checked )
{
    m_timer->stop();
}

void MainWindow::slotTimeout()
{
    for ( int i = 0; i < 5; i++ )
    {
        int value = qrand() % 100;
        dm0[i]->sendValue ( value );
    }

    for ( int i = 5; i < 10; i++ )
    {
        int value = qrand() % 100 * -1;
        dm0[i]->sendValue ( value );
    }
}

void MainWindow::slotFindServer ( QString ip, int port )
{

    for ( int i = 0; i < 10; i++ )
    {
        dm0[i]->setServerAddress ( QHostAddress ( ip ), port );
    }
}
